package com.link.play.fit.ai.ui.main.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.RecyclerView
import com.link.play.fit.ai.R
import com.link.play.fit.ai.bean.FoodInfoBean
import com.link.play.fit.ai.bean.RecentlyEatenData
import com.link.play.fit.ai.bean.toFoodInfoBean
import com.link.play.fit.ai.databinding.ListFoodDatabaseBinding

class FoodDatabaseAcAdapter : RecyclerView.Adapter<FoodDatabaseAcAdapter.ViewHolder>() {

    //添加食物
    var addFood: ((FoodInfoBean) -> Unit)? = null

    //打开食物编辑页面
    var openEditFood: ((FoodInfoBean) -> Unit)? = null

    private var data: MutableList<FoodInfoBean> = mutableListOf<FoodInfoBean>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val bind =
            ListFoodDatabaseBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(bind)
    }

    override fun onBindViewHolder(
        holder: ViewHolder, position: Int
    ) {
        val str = data[position]
        holder.setData(str)
    }

    override fun getItemCount() = data.size

    //设置数据
    fun setData(list: List<FoodInfoBean>?) {
        data = if (list == null) {
            mutableListOf<FoodInfoBean>()
        } else {
            list as MutableList<FoodInfoBean>
        }
        notifyDataSetChanged()
    }

    fun setRecentlyData(list: MutableList<RecentlyEatenData>?) {
        data.clear()
        if (list != null) {
            list.forEach {
                val fb = it.toFoodInfoBean()
                fb.ingredients.first().apply {
                    //把时间戳置空,这是一个新的食物
                    timeMillisencond = ""
                }
                data.add(fb)
            }
        }
        data.reverse()
        notifyDataSetChanged()
    }

    fun clear() {
        data.clear()
        notifyDataSetChanged()
    }

    inner class ViewHolder(val item: ListFoodDatabaseBinding) : RecyclerView.ViewHolder(item.root) {
        var context: Context

        init {
            context = item.root.context
        }

        fun setData(food: FoodInfoBean) {
            val ing = food.ingredients.first()
            val servingType = ing.servingTypes.find {
                if (it.id == ing.servingTypeId) {
                    return@find true
                } else {
                    return@find false
                }
            }
            item.tvTitle.text = ing.name
            item.tvTips.text = ing.calories.toInt().toString() + " cal·${servingType?.metricUnit}"
            item.root.setOnClickListener {
                openEditFood?.invoke(food)
            }
            item.ivAdd.setOnClickListener {
                if (food.select){
                    openEditFood?.invoke(food)
                }else{
                    food.select = true
                    selected(food.select)
                    addFood?.invoke(food)
                }
            }
            selected(food.select)
        }

        //设置数据的选中状态
        fun selected(f: Boolean) = if (f) {
            item.root.isSelected = true
            item.tvTitle.setTextColor("#FFFFFF".toColorInt())
            item.tvTitle.setTextColor("#FFFFFF".toColorInt())

        } else {
            item.root.isSelected = false
            item.tvTitle.setTextColor(
                ResourcesCompat.getColor(
                    context.resources,
                    R.color.color_1d1a23,
                    null
                )
            )
            item.tvTips.setTextColor(
                ResourcesCompat.getColor(
                    context.resources,
                    R.color.color_1d1a23,
                    null
                )
            )
        }
    }
}